<?php

namespace app\admin\controller;

use app\admin\model\RoomModel;
use app\admin\model\SchoolModel;
use app\admin\model\StudentModel;
use cmf\controller\AdminBaseController;
use app\admin\model\ArticleModel;
use think\Db;
use think\db\Query;
use think\Request;

class ArticleController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //搜索
        $tou_time = input('tou_time');
        $wei_time = input('wei_time');
        $title = $this->request->param('title');
        $article = Db::name('article')
            ->join('jf_student', 'jf_student.id=jf_article.author', 'LEFT')
            ->field('jf_article.*,jf_student.name as student_name')
            //选取匹配的条件
            //query 查询
            ->where(function (Query $query) use ($title, $tou_time, $wei_time) {
                if ($title) {
                    $query->where('jf_article.title', 'like', "%$title%");
                }
                if ($tou_time) {
                    $query->where('jf_article.create_time', '>=', $tou_time);
                }
                if ($wei_time) {
                    $query->where('jf_article.create_time', '<=', $wei_time);
                }
            })
            //排序
            ->order("id DESC")
            //paginate 每页数量,是否简洁分页,分页参数
            //request 发送数据
            //param 自动判断是什么类型的数据 就行接收
            ->paginate(2, false, ['query' => request()->param()]);
        //渲染模板
        $page = $article->render();
        return view('index', ['article' => $article, 'page' => $page]);
    }
    public function add()
    {
        //从数据库中选取数据
        $room =RoomModel::select();
        $school = SchoolModel::select();
        return view('add',['school' => $school,'room' => $room,]);
    }
    public function addPost()
    {
        //判断提交类型
        if ($this->request->isPost()){
            //获取提交数据
            $data=input();
            //date 函数格式化本地日期和时间，并返回格式化的日期字符串。
            //time 返回当前时间的 Unix 时间戳，并格式化为日期：
            $data['create_time']=date('Y-m-d H:i:s',time());
            $add=ArticleModel::create($data);
            //判断
            if ($add) {
                //正确处理，默认跳转时间3秒
                $this->success('添加成功',url('article/index'));
            } else {
                //错误处理，默认跳转时间1秒
                $this->error('添加失败');
            }
        }
    }


    public function getRoom(){
        $schoolId=input();
        $room=RoomModel::where(['school_id'=>$schoolId])->select();
        $options=["<option value=''>--请选择班级--</option>"];
        if ($room){
            foreach ($room as $v){
                $opt='<option value="'.$v['id'].'">'.$v['name'].'</option>';
                //向第一个参数的数组尾部添加一个或多个元素，然后返回新数组的长度。
                array_push($options,$opt);
            }
            return $options;
        }else{
            return ["<option value=''>暂无数据</option>"];
        }
    }
    //根据班级id查询该班级下面的学生
    public function getStudent()
    {
        $roomId=$this->request->post();
        $student=Db::name('student')->where(['room_id'=>$roomId])->select();
        $options=["<option value=''>--请选择学生--</option>"];
        if ($student){
            foreach ($student as $v){
                $opt='<option value="'.$v['id'].'">'.$v['name'].'</option>';
                array_push($options,$opt);
            }
            return $options;
        }else{
            return ["<option value=''>暂无数据</option>"];
        }
    }


    public function edit($id)
    {
        $id=input('id');
        $article=ArticleModel::get($id);
        //查询所有的记录
        $school=SchoolModel::all();
        //匹配指定的id
        $room_id=StudentModel::where('id',$article->author)->value('room_id');
        $school_id=RoomModel::where('id',$room_id)->value('school_id');
        $room=RoomModel::where('school_id',$school_id)->select();
        $student=StudentModel::where('room_id',$room_id)->select();
        //实例化
        return view('',[
            'article'=>$article, 'school'=>$school,
            'room_id'=>$room_id, 'school_id'=>$school_id,
            'room'=>$room, 'student'=>$student
        ]);
    }

    public function editpost()
    {
        if ($this->request->isPost()) {
            $data = input();
            unset($data['photo_name']);
            $data['create_time'] = date('Y-m-d H:i:s',time());
            //根据id修改
            $edit=ArticleModel::update($data,['id'=>$data['id']]);
            if ($edit){
                //正确处理，默认跳转时间3秒
                $this->success('修改成功',url('article/index'));
            }else{
                //错误处理，默认跳转时间1秒
                $this->error('修改失败',url('article/index'));

             }
        }
}
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //根据id进行删除
        $del=ArticleModel::destroy($id);
        if ($del){
            //正确处理，默认跳转时间3秒
            $this->success('修改成功',url('article/index'));
        }else{
            //错误处理，默认跳转时间1秒
            $this->error('修改失败',url('article/index'));
        }
    }
    public function read($id)
    {
        $read = Db::name('article')->find($id);
        //接受并返回num的值
        $num = $read['num']+1;
        Db::name('article')
            ->where('id', $id)
            //修改浏览次数
            ->update(['num' => $num]);
        $student = Db::name('student stu')
            ->field('stu.name')
            ->where('id', $read['author'])
            ->find();
        return view('',['read' => $read, 'stu' => $student, 'num' => $num]);
    }
}
